home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte15 / invrect.c < prev    next >
C/C++ Source or Header  |  1995-12-17  |  9KB  |  293 lines

  1.  
  2. #include <windows.h>  
  3. #include "invrect.h"  
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName = "MyApp";
  19. LPCTSTR lpszTitle   = "InvertRect()"; 
  20.  
  21.  
  22. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  23.  
  24.  
  25. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  26.                       LPTSTR lpCmdLine, int nCmdShow)
  27. {
  28.    MSG      msg;
  29.    HWND     hWnd; 
  30.    WNDCLASS wc;
  31.  
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    hWnd = CreateWindow( lpszAppName, 
  54.                         lpszTitle,    
  55.                         WS_OVERLAPPEDWINDOW, 
  56.                         CW_USEDEFAULT, 0, 
  57.                         CW_USEDEFAULT, 0,  
  58.                         NULL,              
  59.                         NULL,              
  60.                         hInstance,         
  61.                         NULL               
  62.                       );
  63.  
  64.    if ( !hWnd ) 
  65.       return( FALSE );
  66.  
  67.    ShowWindow( hWnd, nCmdShow ); 
  68.    UpdateWindow( hWnd );         
  69.  
  70.    while( GetMessage( &msg, NULL, 0, 0) )   
  71.    {
  72.       TranslateMessage( &msg ); 
  73.       DispatchMessage( &msg );  
  74.    }
  75.  
  76.    return( msg.wParam ); 
  77. }
  78.  
  79.  
  80. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  81. {
  82.    WNDCLASSEX wcex;
  83.  
  84.    wcex.style         = lpwc->style;
  85.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  86.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  87.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  88.    wcex.hInstance     = lpwc->hInstance;
  89.    wcex.hIcon         = lpwc->hIcon;
  90.    wcex.hCursor       = lpwc->hCursor;
  91.    wcex.hbrBackground = lpwc->hbrBackground;
  92.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  93.    wcex.lpszClassName = lpwc->lpszClassName;
  94.  
  95.    // Added elements for Windows 95.
  96.    //...............................
  97.    wcex.cbSize = sizeof(WNDCLASSEX);
  98.    wcex.hIconSm = LoadIcon(wcex.hInstance, "SMALL");
  99.             
  100.    return RegisterClassEx( &wcex );
  101. }
  102.  
  103. #define NO_BUTTON      0
  104. #define BUTTON_RECT    1
  105. #define BUTTON_RGN     2
  106.  
  107.  
  108. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  109. {
  110. static POINT pt;
  111. static RECT  rcButton;
  112. static HRGN  hRgnButton     = NULL;
  113. static BOOL  bHighlite      = FALSE;
  114. static int   nButtonPressed = NO_BUTTON;
  115.  
  116.    switch( uMsg )
  117.    {
  118.       case WM_CREATE :
  119.               SetRect( &rcButton, 10, 10, 130, 40 );
  120.               hRgnButton = CreateEllipticRgn( 150, 10, 200, 60 );
  121.               break;
  122.  
  123.       case WM_PAINT : // Draw the buttons.
  124.               {
  125.                  PAINTSTRUCT ps;
  126.  
  127.                  BeginPaint( hWnd, &ps );
  128.  
  129.                  FrameRect( ps.hdc, &rcButton, GetStockObject( BLACK_BRUSH ) );
  130.                  FrameRgn( ps.hdc, hRgnButton, GetStockObject( BLACK_BRUSH ), 1, 1 );
  131.                  TextOut( ps.hdc, 20, 16, "Button A", 8 );
  132.                  TextOut( ps.hdc, 170, 27, "B", 1 );
  133.  
  134.                  EndPaint( hWnd, &ps );
  135.               }
  136.               break;
  137.  
  138.       case WM_LBUTTONDOWN :
  139.               pt.x = LOWORD( lParam );
  140.               pt.y = HIWORD( lParam );
  141.  
  142.               // Check if button A was pressed.
  143.               //...............................
  144.               if ( PtInRect( &rcButton, pt ) )
  145.               {
  146.                  HDC hDC = GetDC( hWnd );
  147.  
  148.                  // Select the button.
  149.                  //...................
  150.                  InvertRect( hDC, &rcButton ); 
  151.                  ReleaseDC( hWnd, hDC );
  152.  
  153.                  bHighlite = TRUE;
  154.                  nButtonPressed = BUTTON_RECT;
  155.                  SetCapture( hWnd );
  156.               }
  157.  
  158.               // Check if button B was pressed.
  159.               //...............................
  160.               if ( PtInRegion( hRgnButton, LOWORD( lParam ), HIWORD( lParam ) ) )
  161.               {
  162.                  HDC hDC = GetDC( hWnd );
  163.  
  164.                  // Select the button.
  165.                  //...................
  166.                  InvertRgn( hDC, hRgnButton );
  167.                  ReleaseDC( hWnd, hDC );
  168.  
  169.                  bHighlite = TRUE;
  170.                  nButtonPressed = BUTTON_RGN;
  171.                  SetCapture( hWnd );
  172.               } 
  173.               break;
  174.  
  175.        
  176.               // If the user moves the mouse cursor out of the 
  177.               // button, the button is un-highlited, otherwise,
  178.               // the button is highlited.
  179.               //...............................................
  180.       case WM_MOUSEMOVE :
  181.               if ( nButtonPressed != NO_BUTTON )
  182.               {
  183.                  HDC hDC = GetDC( hWnd );
  184.  
  185.                  pt.x = LOWORD( lParam );
  186.                  pt.y = HIWORD( lParam );
  187.  
  188.                  switch ( nButtonPressed )
  189.                  {
  190.                     case BUTTON_RECT : 
  191.                           if ( PtInRect( &rcButton, pt ) && !bHighlite )
  192.                           {
  193.                              InvertRect( hDC, &rcButton );
  194.                              bHighlite = TRUE;
  195.                           }
  196.                           if ( !PtInRect( &rcButton, pt) && bHighlite )
  197.                           {
  198.                              InvertRect( hDC, &rcButton );
  199.                              bHighlite = FALSE;
  200.                           }
  201.                           break;
  202.  
  203.                     case BUTTON_RGN :
  204.                           if ( PtInRegion( hRgnButton, pt.x, pt.y ) && !bHighlite )
  205.                           {
  206.                              InvertRgn( hDC, hRgnButton );
  207.                              bHighlite = TRUE;
  208.                           }
  209.                           if ( !PtInRegion( hRgnButton, pt.x, pt.y) && bHighlite )
  210.                           {
  211.                              InvertRgn( hDC, hRgnButton );
  212.                              bHighlite = FALSE;
  213.                           }
  214.                           break;
  215.                  }
  216.                  ReleaseDC( hWnd, hDC );
  217.               }
  218.               break;
  219.  
  220.               // The user let off the mouse button, un-highlite the button.
  221.               //...........................................................
  222.       case WM_LBUTTONUP :
  223.            if ( nButtonPressed != NO_BUTTON )
  224.            {
  225.               if ( bHighlite )
  226.               {
  227.                  HDC hDC = GetDC( hWnd );
  228.  
  229.                  switch( nButtonPressed )
  230.                  {
  231.                     case BUTTON_RECT : InvertRect( hDC, &rcButton ); break;
  232.                     case BUTTON_RGN  : InvertRgn( hDC, hRgnButton ); break;
  233.                  }
  234.                  ReleaseDC( hWnd, hDC );
  235.                  bHighlite = FALSE;
  236.               }
  237.               
  238.               nButtonPressed = NO_BUTTON;
  239.               ReleaseCapture();
  240.            }
  241.            break;
  242.  
  243.       case WM_COMMAND :
  244.               switch( LOWORD( wParam ) )
  245.               {
  246.                  case IDM_ABOUT :
  247.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  248.                         break;
  249.  
  250.                  case IDM_EXIT :
  251.                         DestroyWindow( hWnd );
  252.                         break;
  253.               }
  254.               break;
  255.       
  256.       case WM_DESTROY :
  257.               if ( hRgnButton )
  258.                  DeleteObject( hRgnButton );
  259.  
  260.               PostQuitMessage(0);
  261.               break;
  262.  
  263.       default :
  264.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  265.    }
  266.  
  267.    return( 0L );
  268. }
  269.  
  270.  
  271. LRESULT CALLBACK About( HWND hDlg,           
  272.                         UINT message,        
  273.                         WPARAM wParam,       
  274.                         LPARAM lParam)
  275. {
  276.    switch (message) 
  277.    {
  278.        case WM_INITDIALOG: 
  279.                return (TRUE);
  280.  
  281.        case WM_COMMAND:                              
  282.                if (   LOWORD(wParam) == IDOK         
  283.                    || LOWORD(wParam) == IDCANCEL)    
  284.                {
  285.                        EndDialog(hDlg, TRUE);        
  286.                        return (TRUE);
  287.                }
  288.                break;
  289.    }
  290.  
  291.    return (FALSE); 
  292. }
  293.